package cn.itrigger.publish;

import cn.itrigger.config.VersionConfig;
import cn.itrigger.utils.CommandLineUtils;
import cn.itrigger.utils.GitOperatorUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;

import java.util.*;

public class UatVersionPublish extends ReleaseVersionPublish {
    private VersionConfig config;

    public UatVersionPublish(VersionConfig config) {
        this.config = config;
    }

    @Override
    public Void publish() throws MojoExecutionException {
        String oldVersion = config.getProject().getVersion();
        String newVersion = null;

        if (StringUtils.isNotBlank(config.getFixVersionNumber())) {
            newVersion = config.getFixVersionNumber();
        } else if (Boolean.valueOf(config.getUatIsRelease())) { //uat采用release版本的话,版本号需要自动进行迭代
            if (oldVersion.endsWith(config.getDailySubffix())) {
                newVersion = oldVersion.replace(config.getDailySubffix(),config.getUatSubffix());
            }else if (oldVersion.endsWith(config.getUatSubffix())) {
                String tempVersion = oldVersion.replace(config.getUatSubffix(),"");
                String[] split = StringUtils.split(tempVersion,".");
                if (split.length == 4) {
                    int lastVersionNum = Integer.parseInt(split[3]);//第四位版本号
                    newVersion = StringUtils.joinWith(".",split[0],split[1],split[2],lastVersionNum+1);
                }else {
                    newVersion = StringUtils.joinWith(".",split[0],split[1],split[2],1);
                }
                newVersion = newVersion + config.getUatSubffix();
            }else {
                String[] split = StringUtils.split(oldVersion, ".");
                if (split.length == 4) {
                    newVersion = StringUtils.joinWith(".",split[0],split[1],split[2],Integer.parseInt(split[3])+1);
                }else {
                    newVersion = StringUtils.joinWith(".",split[0],split[1],split[2],1);
                }
                newVersion = newVersion + config.getUatSubffix();
            }
        } else {
            if (oldVersion.endsWith(config.getDailySubffix())) {
                newVersion = oldVersion.replace(config.getDailySubffix(),config.getUatSubffix());
            } else {
                newVersion = oldVersion;
            }
        }


        //如果updateVersion设置了值，则更新版本号，否则不更新
        if (StringUtils.isNotBlank(config.getUpdateVersion()) && Boolean.valueOf(config.getUpdateVersion())) {
            updateDependencyVersion(config.getDailySubffix(), config.getUatSubffix());
            //更新pom.xml文件中的版本号（包括子模块）
            updateVersion(newVersion);
        }

        commitPoms(config);
        GitOperatorUtils.push(" origin " + config.getUatBranch());

        if (StringUtils.isNotBlank(config.getDeploy()) && Boolean.valueOf(config.getDeploy())) {
            deployToNexus(config);
        } else {
            packageToLocal(config);
        }


        return null;
    }

    private void updateDependencyVersion(String dailySubffix, String uatSubffix) {
        Properties properties = config.getProject().getProperties();
        Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();

        for (Map.Entry<Object, Object> entry : entrySet) {
            if (entry.getValue().toString().endsWith(config.getDailySubffix())) {
                if (entry.getValue().toString().endsWith(dailySubffix)) {
                    String result = CommandLineUtils.execCmdWithResult("mvn versions:set-property -Dproperty=" + entry.getKey().toString() + " -DnewVersion=" + entry.getValue().toString().replace(dailySubffix, uatSubffix));
                    System.out.println(result);
                }
            }
        }
    }

}
